package com.bymedia.conexion;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author Arley Duarte
 */
public class InvokeConnection {

    private String mURL = "http://by-media.net/garper/llena_lectura.php?a=";
    private HttpConnection mHttpConnection;


    public InvokeConnection() {
    }

    public String connect(String mensaje) {


        String URL = mURL + mensaje;



        InputStream in = null;

        System.out.println("URL:" + URL);

        try {

            mHttpConnection = (HttpConnection) Connector.open(URL);
            mHttpConnection.setRequestProperty("Connection", "close");
            in = mHttpConnection.openInputStream();


            int contentLength = (int) mHttpConnection.getLength();

            if (contentLength == -1) {
                contentLength = 20000;
            }

            byte[] raw = new byte[contentLength];
            int length = in.read(raw);

            System.out.println(length);

            in.close();
            mHttpConnection.close();

            return new String(raw, 0, length);


        } catch (IOException ioe) {

            ioe.printStackTrace();
            return "";

        }


    }
}
